﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Models;
using BLL;
namespace AGVControlSystemPro
{
    public partial class FrmTrafficEdit : Form
    {
        private TrafficManager objTrafficManager = new TrafficManager();
        public FrmTrafficEdit()
        {
            InitializeComponent();
            this.dgvTraffic.AutoGenerateColumns = false;
            ShowTraffic();
        }
        private void ShowTraffic()
        {
            this.dgvTraffic.DataSource = null;
            this.dgvTraffic.DataSource = objTrafficManager.GetAlltrafficInfo();
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            #region 数据验证
            if (this.nudLandNum.Value == 0)
            {
                if (CommonMethods.LanguageId == 3) MessageBox.Show("地上の表記は0にはならない", "情報のヒント");
                else MessageBox.Show("地标号不能为0", "信息提示");
                return;
            }
            if (this.cboCMDName.SelectedIndex == -1 || this.cboCMDName.Text=="空")
            {
                if (CommonMethods.LanguageId == 3) MessageBox.Show("交通規制の機能を選択してください", "情報のヒント");
                else MessageBox.Show("请选择交管功能", "信息提示");
                return;
            }
            if (this.nudArea.Value == 0)
            {
                if (CommonMethods.LanguageId == 3) MessageBox.Show("交管エリアは0ではありません", "情報のヒント");
                else MessageBox.Show("交管区域不能为0", "信息提示");
                return;
            }
            #endregion

            //数据封装
            TrafficInfo objTraffic = new TrafficInfo()
            {
                   LandNum = Convert.ToInt32(this.nudLandNum.Value),
                   CMDName = this.cboCMDName.Text.Trim(),
                   CMD = this.cboCMDName.SelectedIndex,
                   Area = Convert.ToInt32(this.nudArea.Value)
            };
            try
            {
                int i = objTrafficManager.AddTrafficInfo(objTraffic);
                if (i == 1)
                {
                    ShowTraffic();
                }
            }
            catch (Exception exx)
            {
                
                MessageBox.Show(exx.Message);
            }
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (this.dgvTraffic.CurrentRow == null || this.dgvTraffic.Rows.Count == 0)
            {
                MessageBox.Show("请选择需要修改的数据","修改提示");
                return;
            }
            #region 数据验证
            if (this.nudLandNum.Value == 0)
            {
                if (CommonMethods.LanguageId == 3) MessageBox.Show("地上の表記は0にはならない", "情報のヒント");
                else MessageBox.Show("地标号不能为0", "信息提示");
                return;
            }
            if (this.cboCMDName.SelectedIndex == -1 || this.cboCMDName.Text == "空")
            {
                if (CommonMethods.LanguageId == 3) MessageBox.Show("交通規制の機能を選択してください", "情報のヒント");
                else MessageBox.Show("请选择交管功能", "信息提示");
                return;
            }
            if (this.nudArea.Value == 0)
            {
                if (CommonMethods.LanguageId == 3) MessageBox.Show("交管エリアは0ではありません", "情報のヒント");
                else MessageBox.Show("交管区域不能为0", "信息提示");
                return;
            }
            #endregion
            //数据封装
            TrafficInfo objTraffic = new TrafficInfo()
            {
                ID =Convert.ToInt32( this.dgvTraffic.CurrentRow.Cells["ID"].Value),
                LandNum = Convert.ToInt32(this.nudLandNum.Value),
                CMDName = this.cboCMDName.Text.Trim(),
                CMD = this.cboCMDName.SelectedIndex,
                Area = Convert.ToInt32(this.nudArea.Value)
            };
            try
            {
                int i = objTrafficManager.ModifyTrafficInfo(objTraffic);
                if (i == 1)
                {
                    ShowTraffic();
                }
            }
            catch (Exception exx)
            {

                MessageBox.Show(exx.Message);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.dgvTraffic.CurrentRow == null || this.dgvTraffic.Rows.Count == 0)
            {
                if (CommonMethods.LanguageId == 3) MessageBox.Show("削除しなければならないデータを選択してください。", "情報のヒント");
                else MessageBox.Show("请选择需要删除的数据", "删除提示");
                return;
            }
            string id = this.dgvTraffic.CurrentRow.Cells["ID"].Value.ToString();
            DialogResult result;
            if (CommonMethods.LanguageId == 3) result = MessageBox.Show("削除番号を確認する" + id + "データ？", "情報のヒント", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            else result = MessageBox.Show("确认删除编号" + id + "数据吗？", "删除提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (result == DialogResult.OK)
            {
                try
                {
                    int i = objTrafficManager.DeleteTrafficInfo(id);
                    if (i == 1) ShowTraffic();
                }
                catch (Exception ex)
                {
                    
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void FrmTrafficEdit_FormClosed(object sender, FormClosedEventArgs e)
        {
            FrmMain.objFrmTrafficEdit = null;
        }

        private void dgvTraffic_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dgvTraffic.CurrentRow == null || this.dgvTraffic.Rows.Count == 0) return;
            this.nudLandNum.Value = Convert.ToInt32(this.dgvTraffic.CurrentRow.Cells["LandNum"].Value);
            this.cboCMDName.Text = this.dgvTraffic.CurrentRow.Cells["CMDName"].Value.ToString();
            this.nudArea.Value = Convert.ToInt32(this.dgvTraffic.CurrentRow.Cells["Area"].Value);
        }

        private void dgvTraffic_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Common.DataGridViewStyle.DgvRowPostPaint(this.dgvTraffic,e);
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            List<TrafficInfo> list = objTrafficManager.QueryTrafficInfo(this.nudLandNum.Value.ToString(),this.cboCMDName.Text.Trim(),this.nudArea.Value.ToString());
            this.dgvTraffic.DataSource = null;
            this.dgvTraffic.DataSource = list;
        }
    }
}
